package com.aiyige.page.edituserinfo.adapter;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.ImageView;

import com.aiyige.R;
import com.aiyige.utils.banner.BannerAdapter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.Collections;
import java.util.LinkedList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zhuyakun on 2017/10/25.
 */

public class EditUserBannerAdapter extends BaseQuickAdapter<BannerAdapter.Data, EditUserBannerAdapter.EditUserBannerViewHolder> {
    ItemTouchHelper itemTouchHelper;
    int originalAdapterPosition = -1;
    Listener listener = null;

    public interface Listener {
        void onPositionChanged();
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public EditUserBannerAdapter() {
        super(R.layout.edit_user_banner_item, new LinkedList<BannerAdapter.Data>());
        itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                EditUserBannerViewHolder editUserBannerViewHolder = (EditUserBannerViewHolder) viewHolder;
                if (editUserBannerViewHolder.getData().equals(BannerAdapter.Data.DUMMY_ITEM)) {
                    return 0;
                }
                int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.START | ItemTouchHelper.END;
                int swipeFlags = 0;
                return makeMovementFlags(dragFlags, swipeFlags);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                if (getItem(target.getAdapterPosition()) == BannerAdapter.Data.DUMMY_ITEM) {
                    return false;
                } else {
                    Collections.swap(getData(), viewHolder.getAdapterPosition(), target.getAdapterPosition());
                    notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
                    return true;
                }
            }

            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                switch (actionState) {
                    case ItemTouchHelper.ACTION_STATE_DRAG:
                        viewHolder.itemView.setAlpha(0.618f);
                        originalAdapterPosition = viewHolder.getAdapterPosition();
                        break;
                    default:
                        break;

                }
                super.onSelectedChanged(viewHolder, actionState);
            }

            @Override
            public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                super.clearView(recyclerView, viewHolder);
                viewHolder.itemView.setAlpha(1);
                if (listener != null && viewHolder.getAdapterPosition() != originalAdapterPosition) {
                    listener.onPositionChanged();
                }
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

            }

            @Override
            public boolean isLongPressDragEnabled() {
                return true;
            }

            @Override
            public boolean isItemViewSwipeEnabled() {
                return false;
            }
        });
    }


    @Override
    public void bindToRecyclerView(RecyclerView recyclerView) {
        super.bindToRecyclerView(recyclerView);
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }

    @Override
    protected void convert(EditUserBannerViewHolder helper, BannerAdapter.Data item) {
        helper.bindData(item);
    }

    public static class EditUserBannerViewHolder extends BaseViewHolder {
        @BindView(R.id.addBtn)
        View addBtn;
        @BindView(R.id.coverIv)
        ImageView coverIv;
        BannerAdapter.Data editUserBannerItem;

        public EditUserBannerViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        public BannerAdapter.Data getData() {
            return editUserBannerItem;
        }

        public void bindData(BannerAdapter.Data data) {
            editUserBannerItem = data;
            if (editUserBannerItem.equals(BannerAdapter.Data.DUMMY_ITEM)) {
                coverIv.setVisibility(View.INVISIBLE);
                addBtn.setVisibility(View.VISIBLE);
            } else {
                coverIv.setVisibility(View.VISIBLE);
                addBtn.setVisibility(View.INVISIBLE);
                Glide.with(itemView.getContext())
                        .load(editUserBannerItem.getUrl())
                        .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
                        .into(coverIv);
            }
        }
    }
}
